package com.fpost.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.fpost.dao.GenericDAO;
import com.fpost.domain.ArticleComment;

public class ArticleCommentDAO extends GenericDAO<ArticleComment> {
	
	@Override
	public Long save(ArticleComment articleComment) throws HibernateException {
		Long articleCommentId = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			articleCommentId = (Long) session.save(articleComment);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return articleCommentId;
	}

	@Override
	public void delete(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(get(id));
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public void delete(ArticleComment articleComment) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(articleComment);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public ArticleComment get(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		ArticleComment articleComment = null;
		try {
			session.beginTransaction();
			articleComment = (ArticleComment) session.get(ArticleComment.class, id);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return articleComment;
	}


	@Override
	public void saveOrUpdate(ArticleComment articleComment) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.saveOrUpdate(articleComment);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}
	
	@SuppressWarnings("unchecked")
	public List<ArticleComment> getAll() throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleComment> articleComments;
		try {
			articleComments = (List<ArticleComment>) session.createCriteria(ArticleComment.class).list();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw e;
		}
		return articleComments;		
	}
	
	@SuppressWarnings("unchecked")
	public List<ArticleComment> getCommentsByArticleId(long articleId) throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleComment> articleComments;
		try {
			articleComments = (List<ArticleComment>)session.createCriteria(ArticleComment.class)
					.add(Restrictions.eq("article.articleId", articleId))
					.addOrder(Order.desc("createdOn"))
					.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw e;
		}
		return articleComments;		
	}
}
